package com.example.gybicycle.common.util;

import com.example.gybicycle.entity.MemberOrder;

import java.math.BigDecimal;
import java.util.Date;

public class PayUtil {
//    起步金额
    public static BigDecimal Starting_price=new BigDecimal(50);
//    起步时间
    public static int Starting_minuite=40;
//    每次费用
    public static BigDecimal time_price=new BigDecimal(25);
//    xx分钟/次
    public static int time_minuite=60;
//    故障不计费时间（分钟）
    public static int fault_minuite=6;

    /**
     * 正常计费
     * @param druation
     * @return
     */
    public static BigDecimal getMoney(int druation){
        BigDecimal money=Starting_price;
        if(Starting_minuite>=druation)
            return  money;
        druation=druation-Starting_minuite;
        int number=druation/time_minuite;
        if(druation%time_minuite!=0)
            number++;
        money=money.add(time_price.multiply(new BigDecimal(number)));
        return money;
    }

    public static BigDecimal getPrice(MemberOrder order){
        if(order.getStartTime()==null)
            return new BigDecimal(0);
        if(order.getPriceType()==null)
            order.setPriceType(0);
        Date endDate=new Date();
        if(order.getEndTime()!=null)
            endDate=order.getEndTime();
        long menuite=DataUtil.DurationMinutes(order.getStartTime(),endDate);

        if(menuite<=fault_minuite)
            return new BigDecimal(0);
       long hour= menuite/time_minuite;
       if(menuite%time_minuite!=0)
           hour++;

        BigDecimal money;
        if(order.getPriceType().equals(0)){
            money=new BigDecimal(hour).multiply(time_price);
        }else {
             long add=hour-12;
             long num=hour/6;
             if(hour%6!=0)
                 num++;
             if(num>2)
                 num=2;
             money=new BigDecimal(num).multiply(new BigDecimal(80));
             if(add>0){
                 money=money.add(new BigDecimal(add).multiply(time_price));
             }
        }
        System.out.println("order-"+order.getId()+"-hour-"+hour+"-money-"+money);
        return money;
    }

    /**
     * 故障计费
     * @param druation
     * @return
     */

    public static  BigDecimal getFailtrueMoney(int druation){
//        未超过故障不计费时间 不计费
        if(druation<=fault_minuite)
            return new BigDecimal(0);
        return getMoney(druation);
    }
}
